package com.eascs.web.p2p.loginAction;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.app.dao.PageResult;
import com.eascs.common.util.UrlUtil;
import com.eascs.p2p.common.entity.AdminRoleEntity;
import com.eascs.p2p.common.entity.AdminUserEntity;
import com.eascs.p2p.common.service.IAdminRoleService;
import com.eascs.p2p.common.service.IAdminUserService;
import com.eascs.p2p.common.vo.AdminUserVO;
import com.eascs.p2p.common.vo.ModelResult;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxButton;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.RedirectButton;
import com.eascs.site.view.button.ShowButtonFilter;
import com.eascs.tag.MenuTag;
import com.eascs.web.p2p.common.BaseMasterViewController;

@Controller
@RequestMapping("/admin/user")
public class AdminController extends BaseMasterViewController {

	@Reference
	IAdminUserService adminUserService;
	
	@Reference
	private IAdminRoleService  iAdminRoleService;
	@Override
	public void preHandle() {
		super.preHandle();
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "admin");
	}
	/**
	 * @Title: index
	 * @Description: banner图列表
	 * @author 张庆奇
	 * @param vo
	 * @return
	 * @return String
	 * @throws
	 * @date 2016年7月11日 下午5:05:45
	 */
	@RequestMapping(value = "/index")
	public String index(AdminUserVO vo) {

		vo.setPageSize(10);
		ModelResult<PageResult<Object[]>> model = adminUserService.findPageAdminUserEntity(vo);
		PageResult<Object[]> pageResult=new PageResult<Object[]>();
		if(model.isSuccess()){
			 pageResult = model.getModel();
		} 
			
		

		String[] titles = { "真实姓名", "手机号",  "用户状态" ,"创建日期"};
		
		AjaxButton openButton=new AjaxButton("启用                   ", "/admin/user/Rest/updateStatus?status=0&id=");
		openButton.setShowButtonFilter(new ShowButtonFilter(){

			@Override
			public boolean filter(Object param) {
				Object[] params=(Object[]) param;
				return "停用".equals(params[3]);
			}
			
		});
		
		AjaxButton closeButton=new AjaxButton("禁用                   ", "/admin/user/Rest/updateStatus?status=1&id=");
		closeButton.setShowButtonFilter(new ShowButtonFilter(){

			@Override
			public boolean filter(Object param) {
				Object[] params=(Object[]) param;
				return "启用".equals(params[3]);
			}
			
		});
		

		
		RedirectButton updateButton=new RedirectButton("修改                   ", "/admin/user/edit_admin?id=");
		RedirectButton roleButton=new RedirectButton("添加角色                   ", "/admin/user/add_role?id=");
		Button[] detailButton = new Button[] {openButton,closeButton,updateButton,roleButton};
		String pageUrl = "/admin/user/index";

		
		pageUrl = UrlUtil.appendUrl(pageUrl, "name", vo.getName());
		pageUrl = UrlUtil.appendUrl(pageUrl, "mobile", vo.getMobile());
		pageUrl = UrlUtil.appendUrl(pageUrl, "status", vo.getStatus());
		pageUrl = UrlUtil.appendUrl(pageUrl, "beginCreateTime", vo.getBeginCreateTime());
		pageUrl = UrlUtil.appendUrl(pageUrl, "endCreateTime", vo.getEndCreateTime());
		pageUrl = UrlUtil.appendUrl(pageUrl, "showPage", "");

		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, pageUrl );
		paginationInfo.setBuildPagingNavigation(true);
		// paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setMoreButtons(detailButton);
		ViewContext.put("paginationInfo", paginationInfo);
		ViewContext.put("vo", vo);
		
		BaseRestResult.refresh();
		return "/admin/index";
	}
	
	@RequestMapping(value = "/add_admin")
	public String addAdmin() {
		ViewContext.put("title", "添加");
		return "/admin/admin_edit";
	}
	
	@RequestMapping(value = "/edit_admin")
	public String editAdmin(String id) {
		ModelResult<AdminUserEntity> model= adminUserService.queryAdminUserById(id);
		AdminUserEntity vo=new AdminUserEntity();
		if(model.isSuccess()){
			vo=model.getModel();
		}
		ViewContext.put("vo", vo);
		ViewContext.put("title", "修改");
		return "/admin/admin_edit";
	}
	
	
	/**
	 * @Title: addRole
	 * @Description:添加角色
	 * @author lwx
	 * @param 用户id
	 * @return
	 * @return String
	 * @throws
	 * @date 2016年7月11日 下午5:05:45
	 */
	@RequestMapping(value = "/add_role")
	public String addRole(String id) {
		ModelResult<AdminUserEntity> model= adminUserService.queryAdminUserById(id);
		ModelResult<List<AdminRoleEntity>> roleModel= adminUserService.queryRoleList();
		ViewContext.put("user", model.getModel());
		ViewContext.put("roleList", roleModel.getModel());
		
		AdminUserVO vo=new AdminUserVO();
		vo.setPageSize(10);
		vo.setId(id);
		PageResult<Object[]> pageResult = null;
		ModelResult<PageResult<Object[]>> listModel=adminUserService.queryRoleListByUser(vo);
		if(listModel.isSuccess()){
			pageResult=listModel.getModel();
		}
		String[] titles = { "角色编码", "角色名称", "角色描述" };
 		PaginationInfo paginationInfo=new PaginationInfo(pageResult,titles);
 		Button[] button=new Button[]{
 				new AjaxButton("删除", "/admin/user/Rest/delUserRole?userId="+id+"&roleId=")
 		};
 		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setMoreButtons(button);
		ViewContext.put("paginationInfo", paginationInfo);
		
		return "/sys/addUserRole";
	}
	
}
